<?php
/**
 * 站点配置页
 * M2
 * @param
 */
class ConfigAction extends PublicAction {
	
	/**
	 * 站点列表
	 */
	public function siteconfiglist(){
		$list = D('Site')->getSiteListArr('siteconfiglist');
		$value = array_values($list);
		$sid = intval($_GET['sid']) ? intval($_GET['sid']) : $value[0]['siteid'];
		if ($sid > 0) {
			$channelslist = M('cms_templates')->where(array('siteid'=>$sid,'template_type'=>'channels'))->order('id desc')->select();
			$contentslist = M('cms_templates')->where(array('siteid'=>$sid,'template_type'=>'contents'))->order('id desc')->select();
			$changeStyle = model('Data')->get('cmsconfig:site-'.$sid);
			$this->assign ( 'channelslist', $channelslist );
			$this->assign ( 'contentslist', $contentslist);
			$this->assign ( 'changeStyle', $changeStyle );
			$this->assign($changeStyle);
			$this->assign('ssid', $sid);
		}
		$this->assign('list', $list);
		$this->display();
	}
	
	/**
	 * 将站点列表封装成tree
	 * sun
	 */
	public function siteTreeList() {
		echo D('Site')->getSiteList('siteconfiglist');
	}
	
	/**
	 * 站点配置显示
	 */
	public function index() {
		$sid = intval($_GET['sid']);
		$channelslist = M('cms_templates')->where(array('siteid'=>$sid,'template_type'=>'channels'))->order('id desc')->select();
		$contentslist = M('cms_templates')->where(array('siteid'=>$sid,'template_type'=>'contents'))->order('id desc')->select();
		$changeStyle = model('Data')->get('cmsconfig:site-'.$sid);
		$this->assign ( 'channelslist', $channelslist );
		$this->assign ( 'contentslist', $contentslist);
		$this->assign ( 'changeStyle', $changeStyle );
		$this->assign('sid',$sid);
		$this->assign($changeStyle);
		$this->display ();
	}
	
	/**
	 * 站点配置保存
	 */
	public function doSetConfig() {
		if ($_POST['__hash__'])
			unset ($_POST['__hash__']);
		$siteid = intval($_POST['sid']);
		unset($_POST['sid']);
		$_POST['channelTemplate'] = empty($_POST['channelTemplate'])?'channel.html':trim($_POST['channelTemplate']);
		$_POST['contentTemplate'] = empty($_POST['contentTemplate'])?'content.html':trim($_POST['contentTemplate']);
		$_POST['catenaming'] = empty($_POST['catenaming'])?'channels-{cateid}.html':trim($_POST['catenaming']);
		$_POST['artnaming'] = empty($_POST['artnaming'])?'contents-{contentid}.html':trim($_POST['artnaming']);
		$_POST['picSavePath'] = empty($_POST['picSavePath'])?'./data/cms/upload':trim($_POST['picSavePath']);
		
		$arr['site-'.$siteid] = $_POST;
		$result = model('Data')->lput('cmsconfig', $arr);										
		if($result)
			$this->success('配置成功');
	}
	
	/**
	 * 门户又拍云配置
	 */
	public function yunconfig() {
		if($_POST) {
			try {
				//$filename = SITE_PATH . '/apps/cms/Conf/yunconfig.php';
			//	file_exists($filename) or touch($filename);
				if($_POST['__hash__']) unset($_POST['__hash__']);
				//$return = var_export($_POST, TRUE);
				$result = model('Data')->lput( "yunconfig", $_POST);
// 				if ($return) {
// 					file_put_contents($filename, "<?php \r\n return " . $return . ";");
// 				}
				$this->success('配置成功',URL('cms/Config/yunconfig'));
			} catch (Exception $e) {
			//	$this->error("请检查文件{$filename}是否有读写权限");
			}
		} else {
// 			$configname = empty($_GET['configname'])?'yun':$_GET['configname'];
// 			$config = include SITE_PATH . '/apps/cms/Conf/yunconfig.php';
// 			if (!$config) {
// 				$config = array();
// 			}
			$config = model('Data')->lget( "yunconfig");
			$this->assign("config", $config);
			$this->display();
		}
	}
}